package com.newasia.management.services;

import com.newasia.management.ISysMenuService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.converter.SysMenuConverter;
import com.newasia.management.converter.SysRoleConverter;
import com.newasia.management.dal.item.entitys.ItemSku;
import com.newasia.management.dal.item.entitys.ItemSkuExample;
import com.newasia.management.dal.item.entitys.ItemSkuPerm;
import com.newasia.management.dal.item.persistence.ItemSkuMapper;
import com.newasia.management.dal.item.persistence.ItemSkuPermMapper;
import com.newasia.management.dal.user.entitys.SysMenu;
import com.newasia.management.dal.user.entitys.SysRole;
import com.newasia.management.dal.user.persistence.SysMenuMapper;
import com.newasia.management.dal.user.persistence.SysRoleMapper;
import com.newasia.management.dto.user.*;
import com.newasia.management.dto.user.menu.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    ItemSkuMapper itemSkuMapper;
    @Autowired
    ItemSkuPermMapper itemSkuPermMapper;
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRoleConverter sysRoleConverter;

    @Autowired
    SysMenuConverter sysMenuConverter;

    @Override
    public List<SysMenuDto> selectSysMenuByRoleId(Long role_id) {
        log.info("Begin SysMenuServiceImpl.selectSysRoleByUserId: user_id:"+role_id);
        List<SysMenuDto> sysRoleDtoList = new ArrayList<SysMenuDto>();
        SysRole sysUser = new SysRole();
        sysUser.setRoleId(role_id);
        try{
            List<SysMenu> sysRoles = sysMenuMapper.selectSysMenuByRoleId(sysUser.getRoleId());
            if(sysRoles.size()<0){
                return null;
            }
            sysRoleDtoList = sysMenuConverter.menu2List(sysRoles);
        }catch (Exception e){
            log.error("SysMenuServiceImpl.selectSysRoleByUserId Occur Exception :"+e);
        }
        return sysRoleDtoList;
    }

    @Override
    public SysMenuListResponse selectSysMenuList(SysMenuListRequset requset) {
        log.info("Begin SysMenuServiceImpl.selectSysMenuList");
        SysMenuListResponse response = new SysMenuListResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            List<SysMenu> menuList = new ArrayList<SysMenu>();
            if(requset.getMenuId()==null||requset.getMenuId().equals(""))
                menuList = sysMenuMapper.selectSysMenu(requset.getStatus());
            else
                menuList.add(sysMenuMapper.selectByPrimaryKey(requset.getMenuId()));
            List<MenuRoleListDto> dtos = sysMenuConverter.menuRoleList2List(menuList);
            dtos.parallelStream().forEach(dto->{
                dto.setSysRoleDtoList(sysRoleConverter.role2List(
                        sysRoleMapper.selectSysRoleByMenuId(dto.getMenuId())));
            });
            response.setSysMenuDtoList(dtos);
        }catch (Exception e){
            log.error("SysMenuServiceImpl.SysRoleServiceImpl Occur Exception :"+e);
            response.setCode(ManagementRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ManagementRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    @Override
    public UpdateSysMenuResponse updateSysMenu(UpdateSysMenuRequset requset) {
        log.info("Begin SysMenuServiceImpl.updateSysMenu");
        UpdateSysMenuResponse response = new UpdateSysMenuResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());

        try{
            SysMenu menu = sysMenuMapper.selectByPrimaryKey(requset.getMenuId());
            if(menu.getStatus()==1){
                response.setCode(ManagementRetCode.SYSTEM_MENU_CANNOT_REVISE.getCode());
                response.setMsg(ManagementRetCode.SYSTEM_MENU_CANNOT_REVISE.getMessage());
                return response;
            }
            menu = new SysMenu();
            menu.setMenuId(requset.getMenuId());
            menu.setPerms(requset.getPerms().toString());
            menu.setMenuName(requset.getMenuName());
            sysMenuMapper.updateByPrimaryKeySelective(menu);
        }catch (Exception e){
            log.error("SysMenuServiceImpl.updateSysMenu Occur Exception :"+e);
            response.setCode(ManagementRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ManagementRetCode.SYSTEM_ERROR.getMessage());
        }

        return response;
    }

    @Override
    @Transactional
    public InsertSysMenuResponse insertSysMenu(InsertSysMenuRequset requset) {
        log.info("Begin SysMenuServiceImpl.insertSysMenu");
        InsertSysMenuResponse response = new InsertSysMenuResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());

        try{
            SysMenu menu = new SysMenu();
            menu.setMenuName(requset.getMenuName());
            menu.setPerms(requset.getPerms().toString());
            menu.setStatus(0);
            sysMenuMapper.insert(menu);

            ItemSkuExample itemSkuExample = new ItemSkuExample();
            List<ItemSku> itemSkus = itemSkuMapper.selectByExample(itemSkuExample);
            itemSkus.parallelStream().forEach(itemSku -> {
                ItemSkuPerm perm = new ItemSkuPerm();
                perm.setMenuId(menu.getMenuId());
                perm.setSkuId(itemSku.getSkuId());
                perm.setPercentage(BigDecimal.valueOf(requset.getPerms()));
                itemSkuPermMapper.insertSelective(perm);
            });
        }catch (Exception e){
            log.error("SysMenuServiceImpl.insertSysMenu Occur Exception :"+e);
            e.printStackTrace();
            response.setCode(ManagementRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ManagementRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    @Override
    public DeleteSysMenuResponse deleteSysMenu(DeleteSysMenuRequset requset) {
        log.info("Begin SysMenuServiceImpl.deleteSysMenu");
        DeleteSysMenuResponse response = new DeleteSysMenuResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());

        try{
            SysMenu menu = sysMenuMapper.selectByPrimaryKey(requset.getMenuId());
            if(menu.getStatus()==1){
                response.setCode(ManagementRetCode.SYSTEM_MENU_CANNOT_DELETE.getCode());
                response.setMsg(ManagementRetCode.SYSTEM_MENU_CANNOT_DELETE.getMessage());
                return response;
            }
            sysMenuMapper.deleteByPrimaryKey(requset.getMenuId());
        }catch (Exception e){
            log.error("SysMenuServiceImpl.deleteSysMenu Occur Exception :"+e);
            response.setCode(ManagementRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ManagementRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }
}
